package com.chuanjiabao.wisdom.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.provider.MediaStore;
import android.util.Base64;
import android.webkit.MimeTypeMap;

import com.chuanjiabao.wisdom.application.FApplication;

import java.io.ByteArrayOutputStream;

/**
 * Created by 86178 on 2019/8/19.
 * author mogen
 * 用心，成就一切。
 */

public class ImageUtil {

    /**
     * 获取视频文件截图
     *
     * @param path 视频文件的路径
     * @return Bitmap 返回获取的Bitmap
     */

    public static Bitmap getVideoThumb(String path) {
        MediaMetadataRetriever media = new MediaMetadataRetriever();
        media.setDataSource(path);
        return media.getFrameAtTime();
    }


    public static boolean imgOrVideo(Context context, String imagePath) {
        boolean isImg = true;
        String fileExtension = MimeTypeMap.getFileExtensionFromUrl(imagePath);
        String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
        if (mimeType != null && mimeType.contains("video")) {
            isImg = false;
        }
        return isImg;
    }

    public static String getFileName(String filePath){
        String fileName = "";
        String temp[] = filePath.replaceAll("\\\\","/").split("/");
        if (temp.length > 1) {
            fileName = temp[temp.length - 1];
        }
        return fileName;
    }

    public static String bitmapToString(Bitmap bitmap){
        //将Bitmap转换成字符串
        String string=null;
        ByteArrayOutputStream bStream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG,100,bStream);
        byte[]bytes=bStream.toByteArray();
        string= Base64.encodeToString(bytes,Base64.DEFAULT);
        return string;
    }


}
